if (window.plus) {
    ready();
} else {
    document.addEventListener('plusready', ready, false);
}
$(function(){
  if(AGENT_CLIENT.WEB){
    ready();
  } 
})
function ready(){

    var _thisPage = null;
    var _user = null;
    var _type = '';
	var _backurl = getUrlParam('backurl');
    var setphonePage = function () {
        this.init();
    };
    setphonePage.prototype = {
        init: function () {
            this.initData();
            this.bindEvent();
        },
        initData: function () {
            _user = getLocalStorage(STORAGE_MANAGER.user);
            if (!_user) {
                redirect("login.html");
            }
            _type = getUrlParam('type');
            if('edit' == _type){
            	$("#title").html("修改手机号码");
            	document.title = "修改手机号码";
            }
        },
        bindEvent: function () {
            //点击回退
            $("#back").on("click", function () {
                back();
            });

            //清除输入
            $("#clear").on("click", function () {
                $("input").val("");
            });

            //点击获取验证码
            $("#getVcode").on("click", function () {
            	if( !isTelephone( $("#newPhone").val() ) ){
            		$.alert("请填写手机号");
            		return;
            	}
            	$.ajax({
            		type:"POST",
            		url: CONFIG_MANAGER.RequstUrl + "v1/account/check",
            		async: true,
            		contentType:"application/json",
            		data: JSON.stringify( $("#newPhone").val() )
            	}).done(function(data){
            		if(data.succeed){
            			$.alert('手机号已经存在');
            		}else{
            			change_code($("#getVcode"), $("#newPhone"), SMS_TYPE.verify);
            		}
            	}).fail(function(data){
            		var infor = JSON.parse(data.responseText);
            		$.toast(infor.Message);
            	});
            });

            $("#commit").on("click", function () {
                if ("" == $("#vcode").val()) {
                    $.alert("请填写验证码");
                    return false;
                }
                if (!isVCode($("#vcode").val())) {
                    $.alert("请填写正确的验证码");
                    return false;
                }

                var postData = {
                    "memberId": _user.id,
                    "vcode": $("#vcode").val(),
                    "origin_obj": "",
                    "active_obj": $("#newPhone").val()
                };
                $.ajax({
                    type: "POST",
                    url: CONFIG_MANAGER.RequstUrl + "v1/member/safety/phone",
                    async: true,
                    data: postData,
                    headers: {"token": _user.token},
                    beforeSend: function () {
                        $.showWaiting('等待中...');
                    }
                }).done(function (data) {
	                $.alert("更新成功", function(){
	                	//重新获取用户信息
			            $.ajax({
			                headers: { "token": _user.token },
			                type: "GET",
			                url: CONFIG_MANAGER.RequstUrl + "v1/member/" + _user.id,
			                async:true
			            }).done(function(data){
			            	setLocalStorage(STORAGE_MANAGER.currentmember, data);
			            }).always(function(data){
		                  	$.closeWaiting();
		               	});	                	
	                	if(_backurl){
	                		var redirectUrl = "../" + _backurl.replace("|", "/");
	                		redirect(redirectUrl);
	                	}else{
	                		redirect("account.html");
	                	}
	                });
                }).fail(function(data){
                	$.toast(data.responseJSON.Message);
                });

            });
        },
    };

    _thisPage = new setphonePage();

}